package com.wqm.step.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wqm.step.pojo.Student;
import com.wqm.step.pojo.Teacher;
import com.wqm.step.service.ManyToManyService;

@Controller
public class ManyToManyController {

	@Autowired
	private ManyToManyService manyToManyService;

	/**
	 * 多对多，级联保存
	 * 多对多最好两边都配置级联,详细看Customer类中级联讲解
	 * @return
	 */
	@RequestMapping(value = "saveManyToMany.action", produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String saveManyToMany() {
		// 1.创建两个老师
		Teacher t1 = new Teacher();
		t1.setName("tom老师");

		Teacher t2 = new Teacher();
		t2.setName("fox老师");

		// 2.创建两个学生
		Student s1 = new Student();
		s1.setName("张三同学");

		Student s2 = new Student();
		s2.setName("张龙同学");

		manyToManyService.saveManyToMany(t1, t2, s1, s2);
		return "数据保存成功？ 看看吧";
	}
	
	/**
	 * 多对多， 级联删除 (前提是建立了双向的级联 All)
	 * 多对多最好两边都配置级联,详细看Customer类中级联讲解
	 * @return
	 *  删除Student表中id为1的数据
	 */
	@RequestMapping(value = "deleteData.action", produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String deleteData() {
		Integer id = 1;
		manyToManyService.deleteData(id);
		return "数据保存成功？ 看看吧";
	}

}
